package app.k.com.jiecaoapp.fragment;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;


import com.viewplug.view.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import app.k.com.jiecaoapp.R;

/**
 * Created by mac_wk on 15-1-7.
 */
public class PictureFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout mSwipeLayout;
    private ListView mListView;
    private ImageView refreshView;
    private Animation refreshAnimation;
    private ArrayList<String> list = new ArrayList<String>();
    private int screen_width;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.content_layout, null);
            initViews();
        }
        screen_width = ScreenUtils.getScreenWidth(getActivity());
        screen_width = screen_width- ScreenUtils.dip2px(getActivity(),(7+32));
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return  rootView;
    }
    private void initViews() {
        refreshAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.refresh_rotate);
        refreshAnimation.setInterpolator(new DecelerateInterpolator());
        refreshView = (ImageView) findViewById(R.id.refreshview);
        mListView = (ListView) findViewById(R.id.listview);
        final List<String> data = getData();
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test1);
        mListView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView==null)
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_item,null);
                int height = bitmap.getHeight();
                ImageView imageView = (ImageView) convertView.findViewById(R.id.content_img);
                int width = screen_width;
                int bitwidth = bitmap.getWidth();
                int img_heiht = width*bitmap.getHeight()/bitwidth;
                Log.d("height",img_heiht+" screen:"+width+"  bitwidth:"+bitwidth+"   img:"+imageView.getMeasuredWidth());
                imageView.getLayoutParams().height=img_heiht;
                imageView.setImageBitmap(bitmap);
                return convertView;
            }
        });
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(R.color.holo_blue_bright,
                R.color.holo_green_light, R.color.holo_orange_light,
                R.color.holo_red_light);
        refreshView.setOnClickListener(baseClick);
    }
    private ArrayList<String> getData() {
        list.add("Hello");

        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("Hello");

        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("Hello");
        list.add("Hello");

        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("Hello");

        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("Hello");

        list.add("An Android Developer");
        list.add("Love Open Source");

        return list;
    }
    @Override
    public void onRefresh() {
        refreshView.startAnimation(refreshAnimation);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                refreshView.clearAnimation();
            }
        }, 5000);
    }
    @Override
    protected void onClickView(View view) {
        switch (view.getId()) {
            case R.id.refreshview:
                onRefresh();
                break;
        }
    }

}
